Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
Close the page after certain interval [Puppeteer]

I have used puppeteer for one of my projects to open webpages in headless chrome, do some actions and then close the page. These actions, however, are user dependent. I want to attach a lifetime to the page, where it closes automatically after, say 30 minutes, of opening irrespective of whether any action is performed or not.

I have tried setTimeout() functionality of Node JS but it didn't work (or I just couldn't figure how to make it work). I have tried the following:

const puppeteer = require('puppeteer-core');

const browser = await puppeteer.connect({browserURL: browser_url});
const page = await browser.newPage();
// timer starts ticking here upon creation of new page (maybe in a subroutine and not block the main thread)

/**
 ..
 Do something
 ..
*/

// timer ends and closePage() is triggered.

const closePage = (page) => {
    if (!page.isClosed()) {
        page.close();
    }
}

But this gives me the following error: Error: Protocol error: Connection closed. Most likely the page has been closed.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your provided code should work as excepted. Are you sure the page is still opened after the timeout and it is indeed the same page?

You can try this wrapper for opening pages and closing them correctly.

// since it is async it won't block the eventloop. 
// using `await` will allow other functions to execute.
async function openNewPage(browser, timeoutMs) {
    const page = await browser.newPage()
    setTimeout(async () => {
        // you want to use try/catch for omitting unhandled promise rejections.
        try {
            if(page.isClosed()) {
                await page.close()
            }
        } catch(err) {
            console.error('unexpected error occured when closing page.', err)
        }
       
    }, timeoutMs)
}

// use it like so.
const browser = await puppeteer.connect({browserURL: browser_url});

const min30Ms = 30 * 60 * 1000
const page = await openNewPage(browser, min30Ms);
// ...

The above only closes the Tabs in your browser. For closing the puppeteer instance you would have to call browser.close() which could may be what you want?

about 4 years ago · Juan Pablo Isaza Denunciar

0

page.close returns a promise so you need to define closePage as an async function and use await page.close(). I believe @silvan's answer should address the issue, just make sure to replace if condition

if(page.isClosed())

with

if(!page.isClosed())
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda